<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="状态" prop="isState">
        <el-select v-model="queryParams.isState" placeholder="指定状态" clearable  size="small">
          <el-option v-for="dict in isStateOptions" :key="dict.val" :label="dict.lable" :value="dict.val" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['admin:one_area_item:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['admin:one_area_item:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['admin:one_area_item:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['admin:one_area_item:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="one_area_itemList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="省" align="center" prop="province" >
        <template slot-scope="scope">
          {{commCode(scope.row.province).data}}
        </template>

      </el-table-column>

      <el-table-column label="市" align="center" prop="city" >
        <template slot-scope="scope">
          {{commCode(scope.row.city).data}}
        </template>

      </el-table-column>

      <el-table-column label="县/区" align="center" prop="area" >
        <template slot-scope="scope">
          {{commCode(scope.row.area).data}}
        </template>

      </el-table-column>

      <el-table-column label="图片" align="center" prop="imgUrl" >
        <template slot-scope="scope">
          <el-image style="width: 100px; height: 100px" :src="scope.row.imgUrl" :preview-src-list="[scope.row.imgUrl]">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="isState" >
        <template slot-scope="scope">
          {{scope.row.isState == 1 ? "展示" : "删除"}}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['admin:one_area_item:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['admin:one_area_item:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改一县一品对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" :before-close="closeView" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-form-item label="图片" prop="imgUrl">
          <el-upload
            class="avatar-uploader"
            :action="fileUrl"
            :show-file-list="false"
            :on-success="successFileQr"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="form.imgUrl" :src="form.imgUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"/>

          </el-upload>        </el-form-item>
        <el-form-item label="视频地址" prop="videoUrl">
          <el-upload class="avatar-uploader"
                     :action="fileUrl"
                     v-bind:on-success="handleVideoSuccess"
                     v-bind:on-progress="uploadVideoProcess"
                     v-bind:show-file-list="false">
            <video controls autoplay id="video"  v-if="form.videoUrl !='' && !videoFlag"
                   v-bind:src="form.videoUrl"
                   class="avatar video-avatar"
                   controls="controls">
              您的浏览器不支持视频播放
            </video>
            <i v-else-if="form.videoUrl =='' && !videoFlag"
               class="el-icon-plus avatar-uploader-icon"></i>
            <el-progress v-if="videoFlag == true"
                         type="circle"
                         v-bind:percentage="videoUploadPercent"
                         style="margin-top:7px;"></el-progress>
          </el-upload>
        </el-form-item>
        <el-form-item label="商品编号" prop="spuId">
          <el-select v-model="form.spuId" placeholder="指定编号" clearable  filterable size="small">
            <el-option v-for="dict in itemAreaListOptions" :key="dict.val" :label="dict.title" :value="dict.val" />
          </el-select>        </el-form-item>

        <el-form-item label="推荐官简介">
          <editor v-model="form.userContent" :min-height="192"/>
        </el-form-item>

        <el-form-item label="状态" prop="isState">
          <el-select v-model="form.isState" placeholder="指定状态" clearable  size="small">
            <el-option v-for="dict in isStateOptions" :key="dict.val" :label="dict.lable" :value="dict.val" />
          </el-select>        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOne_area_item, getOne_area_item, delOne_area_item, addOne_area_item, updateOne_area_item,listOne_area_item_spu, exportOne_area_item } from "@/api/admin/one_area_item";
import Editor from '@/components/Editor';
import { codeConversionName } from "@/api/admin/common";
import { fileUrl } from "@/constant/constant";

export default {
  name: "One_area_item",
  components: { Editor },
  data() {
    return {
      // 遮罩层
      loading: true,
      //是否显示进度条
      videoFlag: false,
      //进度条的进度，
      videoUploadPercent: "",
      // 选中数组
      ids: [],
      fileUrl: "",

      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 1展示 0删除
      isStateOptions:[
        {
          lable:"展示",
          val:1
        },
        {
          lable:"隐藏",
          val:0
        }
      ],
      itemAreaListOptions:[],
      // 总条数
      total: 0,
      // 一县一品表格数据
      one_area_itemList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        province: null,

        city: null,

        area: null,

        userContent: null,

        imgUrl: null,

        videoUrl: null,

        spuId: null,

        isState: 1,

      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },

  watch: {
    // 监听事件，表单页面关闭时结束视频播放
    'visible': function (val) {
      if (this.visible === false) {
        let myVideo = document.getElementById('video')
        myVideo.pause()
      }
    }
  },
  created() {
    this.getList();
    this.fileUrl = fileUrl;
    listOne_area_item_spu().then(res =>{
      this.itemAreaListOptions = res.data;
    })

  },
  methods: {
    //进度条
    uploadVideoProcess(event, file, fileList) {
      this.videoFlag = true;
      this.videoUploadPercent = file.percentage.toFixed(0) * 1;
    },
    /** 查询一县一品列表 */
    getList() {
      this.loading = true;
      listOne_area_item(this.queryParams).then(response => {
        this.one_area_itemList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    closeView(){

    },
    handleVideoSuccess(res, file) {
      this.videoFlag = false;
      this.videoUploadPercent = 0;
      this.form.videoUrl = URL.createObjectURL(file.raw);
      this.form.videoUrl = res.data;
    },
    successFileQr(res, file) {
      this.form.imgUrl = URL.createObjectURL(file.raw);
      this.form.imgUrl = res.data;
    },
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 5;
      if (!isLt2M) {
        this.$message.error("上传图片大小不能超过 5MB!");
      }
      return isLt2M;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,

        province: null,

        city: null,

        area: null,

        userContent: null,

        imgUrl: null,

        videoUrl: null,

        spuId: null,

        createTime: null,

        isState: null,

      };
      this.resetForm("form");
    },
    async commCode(code) {
      return await codeConversionName(code);
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加一县一品";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getOne_area_item(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改一县一品";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOne_area_item(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOne_area_item(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除一县一品编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delOne_area_item(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有一县一品数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportOne_area_item(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.topStyle {
  padding-left: 30px;
}
</style>

<style lang="scss" scoped>
.shopPoi {
  height: 700px;
  width: 1000px;
}
.el-vue-search-box-container {
  width: 100% !important;
  margin-bottom: 10px;
}
</style>
